<template>
  <view class="viewport theme-light">
    <uv-navbar title="" placeholder bgColor="transparent" leftIcon="" :autoBack="true">
      <template #left>
        预约列表
      </template>
    </uv-navbar>

    <uv-sticky class="tabs_content">
      <view class="tabs-container">
        <view class="tabs">
          <view v-for="(item, index) in tabs" :key="index" class="tab-item" :class="{ 'active': index === curTab }"
            @tap="onTabChange(item)">
            {{ item.name }}
          </view>
        </view>
      </view>
    </uv-sticky>

    <view class="container">

      <view class="search_contents">
        <view class="tabs_search">
          <image src="../../static/images/detail/Vector-2.png" style="width: 14px; height: 14px; padding-right: 10px;"
            mode=""></image>
        </view>
        <view class="search_totals">
          <input class="search_totals_input" type="text" placeholder="请输入搜索文案" @input="onInput" />
        </view>
      </view>

      <TabListComponents :items="pagedRecordList.rows" @detail="toDetail"></TabListComponents>

    </view>

  </view>
</template>

<script setup lang="ts">
import { ref, computed, toRaw, unref, reactive } from 'vue'
import { getMemberProfileAPI } from '@/services/profile'
import { getRecords } from '@/services/appointform'
import { onShow, onReachBottom, onLoad } from '@dcloudio/uni-app'
import { useMemberStore, useTenantSystemStore, usePoundStore } from '@/stores'
import { themeColor } from '@/utils/index';
import { TableNameEnum, CURDOperationEnum } from '@/enums/tableCurdEnum'
import mqttStatus, { on as mqttOn, MQTTEvent, connect, getPoundFields, tableCurd, getMainApplication } from '@/utils/mqtt';
import TabListComponents from '@/components/tab-list-components/index.vue'

const memberStore = useMemberStore()
const systemStore = useTenantSystemStore()
const poundStore = usePoundStore()

const listLoading = ref('loading')
const loading = ref(true)

const searchForm = reactive({
  carNo: '',
  status: '',
  code: mqttStatus.code,
  page: 1,
  limit: 10,
})

const onInput = ({ detail }) => {
  searchForm.carNo = detail.value
}

const pagedRecordList = reactive({
  hasNextPage: true,
  totalItemCount: 0,
  rows: []
})

const tabs = ref([{

  name: '全部',
  index: 0,
  searchHandle: () => {
    searchForm.status = ''
  }
}, {

  name: '已审核',
  index: 1,
  searchHandle: () => {
    searchForm.status = 1
  }
}, {

  name: '未审核',
  index: 2,
  searchHandle: () => {
    searchForm.status = 0
  }
},
])
const curTab = ref(0)



const toDetail = (item) => {
  uni.navigateTo({
    url: `/pagesAppointment/detail?id=${item.id}`,
    events: {
      'updated': () => {
        onTabChange(tabs[curTab.value])
      }
    },
    success: ({ eventChannel }) => {
      eventChannel.emit('init', { ...toRaw(unref(item)) })
    },
  })
}


onLoad(() => {

})

onReachBottom(() => {
  if (!pagedRecordList.hasNextPage) {
    return
  }
  searchForm.page++
  getList()
})

const getList = () => {
  if (!memberStore.profile) {
    return
  }

  const handler = tabs.value.find(p => p.index == curTab.value)
  if (handler) {
    handler.searchHandle()
  }

  listLoading.value = 'loading'

  getRecords(searchForm).then(({ rows, hasNextPage }) => {

    listLoading.value = hasNextPage ? 'loadmore' : 'nomore'
    pagedRecordList.hasNextPage = hasNextPage
    if (searchForm.page == 1) {
      pagedRecordList.rows = rows
    } else {
      pagedRecordList.rows = [...pagedRecordList.rows, ...rows]
    }
  }).catch(() => { }).finally(() => {

  })

}

const onTabChange = ({ index, searchHandle }) => {
  curTab.value = index
  searchForm.page = 1
  getList()
}

onShow(() => {
  if (memberStore.profile && memberStore.detailProfile.selectedActiveCode) {
    if (mqttStatus.online) {
      searchForm.code = memberStore.detailProfile.selectedActiveCode
      getList()
    } else {
      uni.showModal({
        title: '提示',
        content: '云服务已离线,请前去主页连接后重试',
      })
    }

  }
})

</script>

<style scoped lang="scss">
@import "@/styles/aj_custom/index.scss";

.tabs-container {
  display: flex;
  justify-content: center;
  font-size: 14px;
  margin-top: 25px;

  .tabs {
    display: flex;

    .tab-item {
      padding: 6px 22px;
      margin-right: 10px;
      cursor: pointer;
    }
  }

  .tab-item.active {
    background-color: rgba(26, 102, 255, 1);
    border-radius: 178px;
    color: white;
  }
}

.search_contents {
  box-shadow: 5px 5px 25px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  margin: 13px 15px;
  padding: 5px 10px;
  border-radius: 10px;
  background-color: white;

  .search_icon {
    margin-left: 5px;
    display: flex;
    // flex: 1;
    padding-right: 10px;
    justify-content: center;
  }

  .search_totals {
    padding: 5px 0;

    .search_totals_input {
      font-size: 12px;
    }
  }
}

.order_notice_detail {
  display: flex;
  justify-content: space-between;
}

.popup_content_masker {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  background-color: black;
  opacity: 0.3;
  padding: 20px;

  .popup_content {
    .popup_content_detail {
      background-color: white;
      margin: 10px;
      padding: 10px;
      border-radius: 5px;
    }

    .popup_search {
      color: white;
      text-align: center;
      background-color: blue;
      padding: 12px 66px;
      border-radius: 10px;
    }
  }
}
</style>
